//
//  SISSRefresh.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/11.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRefresh.h"

@implementation SISSRefresh

+ (instancetype)sharedRefresh {
    static SISSRefresh *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SISSRefresh alloc] init];
    });
    return instance;
}

- (void)prepareHeaderRefresh:(UITableView *)tableView loadDataBlock:(void(^)())block {
    
    //设置mj_header
    self.refreshType = SISSRefressTypeUpDown;
    MJRefreshNormalHeader *header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (block) {
            block();
        }
    }];
//    MJRefreshGifHeader *gifHeader = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//        
//        if (block) {
//            block();
//        }
//    }];
//    
//    gifHeader.stateLabel.hidden = YES;
//    gifHeader.lastUpdatedTimeLabel.hidden = YES;
//    NSMutableArray *headerImages = [NSMutableArray array];
//    for (int i = 1; i <= 4; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"topload%d",i]];
//        [headerImages addObject:image];
//    }
//    [gifHeader setImages:@[headerImages[0]] forState:MJRefreshStateIdle];
//    [gifHeader setImages:headerImages forState:MJRefreshStateRefreshing];
    tableView.mj_header  = header;
    
    // 设置文字
//    [header setTitle:@"刷新成功" forState:MJRefreshStateIdle];
//    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
//    [header setTitle:@"数据刷新中 ..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tableView.mj_header endRefreshing];
    });
    
}

- (void)prepareFooterRefresh:(UITableView *)tableView reloadDataBlock:(void (^)())block {
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (block) {
            block();
        }
    }];
//    footer.automaticallyHidden = YES;
//    MJRefreshAutoGifFooter *gifFooter = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
//        
//        if (block) {
//            block();
//        }
//    }];
//    NSMutableArray *footerImages = [NSMutableArray array];
//    for (int i = 1; i <= 4; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"footerload%d",i]];
//        [footerImages addObject:image];
//    }
//    gifFooter.stateLabel.hidden = YES;
//    gifFooter.refreshingTitleHidden = YES;
//    [gifFooter setImages:@[footerImages[0]] forState:MJRefreshStateIdle];
//    [gifFooter setImages:footerImages forState:MJRefreshStateRefreshing];
    
    tableView.mj_footer = footer;
    
    // 设置文字
//    [footer setTitle:@"刷新成功" forState:MJRefreshStateIdle];
//    [footer setTitle:@"松开刷新" forState:MJRefreshStatePulling];
//    [footer setTitle:@"数据刷新中 ..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:15];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tableView.mj_footer endRefreshing];
    });
}

@end
